<template>
    <div>
       <h1>养老机构新增</h1>
       <table class="table table-bordered">
        <tbody class="bg-info">
            <tr>
                <td align="right">机构名称</td>
                <td>
                    <input type="text" v-model="info.JgName">
                </td>
            </tr>
            <tr>
                <td align="right">机构封面</td>
                <td>
                    <input type="file" @change="img">
                    <img :src="info.JgImg" width="100" height="100" v-if="info.JgImg!=''">
                </td>
            </tr>
            <tr>
                <td align="right">特色服务</td>
                <td>
                    <select v-model="info.SevenName">
                        <option value="">请选择</option>
                        <option v-for="item in xlk" :value="item.JGtypeName">{{item.JGtypeName}}</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td align="right">地址</td>
                <td>
                    <input type="text" v-model="info.Addres">
                </td>
            </tr>
            <tr>
                <td align="right">联系方式</td>
                <td>
                    <input type="text" v-model="info.Phone">
                </td>
            </tr>
            <tr>
                <td align="right">发布状态</td>
                <td>
                    <input type="radio" v-model="info.State" name="state" :value="1">已发布
                    <input type="radio" v-model="info.State" name="state" :value="0">待发布
                </td>
            </tr>
            <tr>
                <td align="right">详情</td>
                <td>
                    <textarea cols="50" rows="5" v-model="info.Desc"></textarea>
                </td>
            </tr>
            <tr>
                <td align="right"></td>
                <td>
                    <input type="button" value="添加" class="btn btn-primary" @click="ok">
                </td>
            </tr>
        </tbody>
       </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive,onMounted} from 'vue';
import axios from 'axios';
import moment from 'mooment';
import{useRouter,useRoute} from 'vue-router';
const router=useRouter();
const route=useRoute();

let info:any=reactive({
  "JgName": "",
  "JgImg": "",
  "SevenName": "",
  "Addres": "",
  "Phone": "",
  "State": 1,
  "Desc": "",
})

//==============特色下拉框===========
let xlk:any=ref([]);

onMounted(()=>{
    gettype();
})

const gettype=()=>{
    axios({
        url:'https://localhost:7268/api/JG/GetJgtype',
        method:'get',
        params:xlk,
    })
    .then(res=>{
        console.log(res);
        xlk.value=res.data;
    })
}
//===========图片上传==========
const img=(e:any)=>{
    let obj=e.target.files[0];
    let f = new FormData();
    f.append("file",obj);

    axios({
        url:'https://localhost:7268/api/Img/Img',
        method:'post',
        data:f,
    })
    .then(res=>{
        console.log(res);
        info.JgImg=res.data;
    })
}
//==============养老新增===========
const ok=()=>{
    if(info.JgName==''){
        alert('机构名称不能为空');
        return;
    }
    if(info.JgImg==''){
        alert('机构封面必选');
        return;
    }
    if(info.SevenName==''){
        alert('特色服务必选');
        return;
    }
    if(info.Addres==''){
        alert('机构地址不能为空');
        return;
    }
    if(info.Phone==''){
        alert('联系方式不能为空');
        return;
    }
    if(info.Desc==''){
        alert('详情不能为空');
        return;
    }

    axios({
        url:'https://localhost:7268/api/JG/AddJg',
        method:'post',
        data:info,
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('添加成功');
            router.push('/jglist');
        }
        else{
            alert('添加失败');
            return;
        }
    })
}
</script>

<style scoped>

</style>